/*
 * $Id: PerforceSyncCompareInput.java 23 2002-09-29 13:59:30Z bpruessmann $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.ui.sync;

import net.sourceforge.p4eclipse.core.PerforceRepositoryProvider;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteSyncElement;
import org.eclipse.team.internal.ui.sync.CatchupReleaseViewer;
import org.eclipse.team.internal.ui.sync.SyncCompareInput;

/**
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PerforceSyncCompareInput extends SyncCompareInput 
{
	private IResource[] resources;
	
	/**
	 * Constructor for PerforceSyncCompareInput.
	 */
	public PerforceSyncCompareInput(IResource[] resources)
	{
		this.resources = resources;
	}

	/*
	 * @see CompareEditorInput#createDiffViewer(Composite)
	 */
	public Viewer createDiffViewer(Composite parent)
	{
		CatchupReleaseViewer catchupReleaseViewer = new PerforceCatchupReleaseViewer(parent, this);
		setViewer(catchupReleaseViewer);
		return catchupReleaseViewer;
	}

	/*
	 * @see SyncCompareInput#createSyncElements(IProgressMonitor)
	 */
	protected IRemoteSyncElement[] createSyncElements(IProgressMonitor monitor)
		throws TeamException
	{
		IRemoteSyncElement[] trees = new IRemoteSyncElement[resources.length];
		int work = 1000 * resources.length;
		monitor.beginTask(null, work);
		try 
		{
			for (int i = 0; i < trees.length; i++) 
			{
				PerforceRepositoryProvider provider = (PerforceRepositoryProvider)RepositoryProvider.getProvider(resources[i].getProject());
				trees[i] = provider.getRemoteSyncTree(resources[i], null, new NullProgressMonitor());
			}
		} 
		finally 
		{
			monitor.done();
		}

		return trees;
	}

	protected void updateView() 
	{
		// Update the view
		if (getDiffRoot().hasChildren()) 
			getViewer().refresh();
		else 
			getViewer().setInput(null);

		// Update the status line
		updateStatusLine();
	}


	/*
	 * @see SyncCompareInput#getSyncGranularity()
	 */
	public int getSyncGranularity()
	{
		return IRemoteSyncElement.GRANULARITY_CONTENTS;
	}

}
